package tabuImpl.tsp.tabuList;

import tabuImpl.tsp.gene.TabuGene;
import tabuInterface.algorithm.tabuList.TabuData;
import tabuInterface.algorithm.tabuList.TabuList;
import tabuInterface.gene.Gene;

public class TabuListImpl implements TabuList {
    private int [] data;//i*n+j  数据由自己组织
    private int nowTime,tabuTime;

    public int getPointN() {
        return pointN;
    }

    private int pointN;

    public int[] getData() {
        return data;
    }

    public TabuListImpl(int n, int tabuTime){//点数量,禁忌有效时长
        this.pointN=n;
        this.tabuTime=tabuTime;
        data=new int [n*(n+1)/2];
        for (int i=0;i<n;i++)
            for (int j=i;j<n;j++)
                data[i*n+j]=-1;
        nowTime=0;
    }

    @Override
    public boolean isTabu(Gene rawGene, Gene newGene) {
        int[] td=((TabuGene)rawGene).checkDiff(newGene);
        return data[td[0]*pointN+td[1]] == -1 || nowTime - data[td[0]*pointN+td[1]] > tabuTime;

    }

    @Override
    public void update(Gene rawGene, Gene newGene) {
        int[] td=((TabuGene)rawGene).checkDiff(newGene);
        data[td[0]*pointN+td[1]]=nowTime;
        nowTime++;
    }
}
